import { defineStore } from 'pinia';

export interface TerminalSpec {
	id: string;
	name: string;
	program?: string;
	args?: string[];
	cwd?: string;
}

export interface TerminalsState {
	activeId: string | null;
	items: TerminalSpec[];
}

export const useTerminalsStore = defineStore('terminals', {
	state: (): TerminalsState => ({ activeId: null, items: [] }),
	actions: {
		create(spec: Omit<TerminalSpec, 'id'> & { id?: string }) {
			const id = spec.id || Math.random().toString(36).slice(2, 10);
			const item: TerminalSpec = { id, name: spec.name || 'Terminal', program: spec.program, args: spec.args, cwd: spec.cwd };
			this.items.push(item);
			this.activeId = id;
			return item;
		},
		remove(id: string) {
			const i = this.items.findIndex(t => t.id === id);
			if (i >= 0) this.items.splice(i, 1);
			if (this.activeId === id) this.activeId = this.items[0]?.id || null;
		},
		activate(id: string) { this.activeId = id; },
	},
});


